<template>
	<div class="shopcart-container">
		<!-- 商品列表区域 -->
		<div style="background-color: white;">
			<van-row v-for="(item, i) in collectionList" :key="i" type="flex" align="center">
				<van-col span="8">
					<!-- 中间商品图片 -->
					<van-icon size="150" :name="APIDomainName + item.pic"></van-icon>
				</van-col>
				<van-col span="16">
					<van-row style="font-size: 18px;margin-bottom: 0.5rem;">{{ item.title }}</van-row>
					<div style="font-size: small;color: #C8C9CC;margin-bottom: 3rem;">1万+人收藏</div>
					<van-row type="flex" align="center">
						<van-col span="8" class="price">¥{{ item.price }}</van-col>
						<van-col span="16" class="opration">
							<van-button style="margin-left: 1rem;border-radius: 5px;color: #979799;" text="删除"
								size="small" @click.prevent="onClickDeleteCollectionItem(item.id)" />
						</van-col>
					</van-row>
				</van-col>
			</van-row>
		</div>
		<div style="margin-top: 2rem;">
			<van-divider :style="{ color: '#fa9d06', borderColor: '#fa9d06', padding: '0 30px' }">
				<van-icon name="like" color="#fa9d06" size="1rem" />
				你可能还喜欢
			</van-divider>
		</div>

		<van-row type="flex" justify="center"
			style="text-align: center;justify-content: space-between;margin-bottom: 1rem;">
			<van-col span="7" class="goods_item" v-for="(item,i) in product" :key="i"
				style='border: 0.05rem solid #ccc; margin-top: 1rem;' @click="onDetail(item.id)">
				<van-image radius="10px 10px 0 0" :src="APIDomainName + item.pic"></van-image>
				<div class="van-ellipsis" style="margin-top: 0.5rem;font-size: 16px;font-weight:600;">{{item.title}}
				</div>
				<div>￥{{item.price}}</div>
			</van-col>
		</van-row>
	</div>
</template>

<script>
	import {
		getCollectionListAPI,
		deleteCollectionItemAPI,
	} from "@/api/collection.js";
	import {
		getProductAPI,
	} from "@/api/product.js";
	export default {
		data() {
			return {
				product: [],
				collectionList: [],
			};
		},
		created() {
			this.getCollectionData();
			getProductAPI().then((result) => {
				console.log(result);
				this.product = result.data;
			});
		},
		methods: {
			getCollectionData() {
				this.collectionList = []; //清空
				getCollectionListAPI().then(result => {
					this.collectionList = result.data;
				}).catch(() => {});
			},
			//触发删除购物商品
			onClickDeleteCollectionItem(id) {
				let ids = id;  
				deleteCollectionItemAPI(ids).then(() => {
					this.getCollectionData();
				});
			},

			// 单击左菜单中的某一项后，显示对应分类的商品列表

			onDetail(id) {
				this.$router.push({
					name: 'productDetail',
					params: {
						id
					},
				});
			}
		}
	};
</script>



<style lang="less" scoped>
	.shopcart-container {
		padding: 0px 10px;

		.cart_header {
			//padding: 10px;
			//margin: 10px 0;
			margin-top: 0.3rem;
			margin-left: 2rem;
			height: 15px;
			background-color: white;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}

	.van-checkbox {
		justify-content: center;
	}

	.title {
		padding: 5px 5px;
		font-size: 12px;
		line-height: 20px;
	}

	.price {
		padding-left: 10px;
		text-align: left;
		font-size: 16px;
		font-weight: bold;
		color: #ffba0b;
		display: flex;
		align-items: center;
	}

	.opration {
		padding-right: 10px;
		display: flex;
		align-items: center;
		justify-content: flex-end;
	}

	.red {
		color: #ffba0b;
		font-weight: bold;
		font-size: 16px;
	}

	.goods_item {
		display: flex;
		flex-direction: column;
		margin: 10px;
		// padding: 5px;
		background-color: white;
		border-radius: 10px;
		font-size: 14px;
		text-align: center;

		.price {
			margin: 0.5rem auto;
		}
	}
</style>
